<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 单位资料认证
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container v2-group">
            <!-- <div class="bulletin">
                <el-popover
                    placement="bottom"
                    trigger="hover">
                    <div class="pop-content statistics">
                        <div>
                            <el-statistic
                                group-separator=","
                                :precision="0"
                                :value="statistics.price"
                                title="总金额"
                            ></el-statistic>
                            <el-statistic
                                group-separator=","
                                :precision="0"
                                :value="statistics.finished_price"
                                title="已完成广告金额"
                            ></el-statistic>
                            <el-statistic
                                group-separator=","
                                :precision="0"
                                :value="statistics.online_price"
                                title="进行中广告金额"
                            ></el-statistic>
                            <el-statistic
                                group-separator=","
                                :precision="0"
                                :value="statistics.not_start_price"
                                title="未开始广告金额"
                            ></el-statistic>
                        </div>
                    </div>
                    <el-row slot="reference" :gutter="20"
                        v-loading="loading"
                        >
                        <el-col :span="6">
                            <div>
                            <el-statistic
                                group-separator=","
                                :precision="0"
                                :value="statistics.total"
                                title="广告总数"
                            ></el-statistic>
                            </div>
                        </el-col>
                        <el-col :span="6">
                            <div>
                            <el-statistic
                                group-separator=","
                                :precision="0"
                                decimal-separator="."
                                :value="statistics.finished_total"
                                title="已完成广告数量"
                            >
                            </el-statistic>
                            </div>
                        </el-col>
                        <el-col :span="6">
                            <div>
                            <el-statistic
                                group-separator=","
                                :precision="0"
                                decimal-separator="."
                                :value="statistics.online_total"
                                title="进行中广告数量"
                            >
                            </el-statistic>
                            </div>
                        </el-col>
                        <el-col :span="6">
                            <div>
                            <el-statistic
                                group-separator=","
                                :precision="0"
                                decimal-separator="."
                                :value="statistics.not_start_total"
                                title="未开始广告数量"
                            >
                            </el-statistic>
                            </div>
                        </el-col>
                    </el-row>
                </el-popover>
            </div> -->
            <div class="handle-box">
                <el-input v-model="query.kw" placeholder="关键词" class="handle-input mr10" clearable></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
                <!-- <el-button
                    type="success"
                    icon="el-icon-plus"
                    class="handle-del mr10"
                    @click="handleAdd"
                >新增广告目标</el-button> -->
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
                stripe
            >
                <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
                <el-table-column prop="username" label="用户名"></el-table-column>
                <el-table-column prop="surname" label="单位名称"></el-table-column>
                <el-table-column prop="user_type" label="用户类型"></el-table-column>
                <el-table-column prop="intro" label="简介" show-overflow-tooltip></el-table-column>
                <el-table-column prop="contact" label="联系人"></el-table-column>
                <el-table-column prop="phone" label="联系方式"></el-table-column>
                <el-table-column prop="vip_level" label="VIP等级"></el-table-column>
                <el-table-column prop="check_stat" width="110" label="状态" sortable>
                    <template slot-scope="scope">
                        <el-tag
                            :type="scope.row.check_stat==='审核通过'?'success':'danger'"
                        >{{scope.row.check_stat}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="160">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-s-flag"
                            @click="handleEdit(scope.$index, scope.row)"
                        >查看审核</el-button>
                        <!-- <el-button
                            type="text"
                            icon="el-icon-view"
                            @click="handleEdit(scope.$index, scope.row)"
                        >查看</el-button> -->
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, sizes, prev, pager, next, jumper"
                    :current-page="query.page"
                    :page-size="query.size"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                    :page-sizes="[10, 20, 30, 40, 50, 100]"
                    @size-change="handleSizeChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="查看与审核" :visible.sync="editVisible" width="30%">
            <el-form ref="entityEdit" :model="form" :rules="rulesEntityEdit" label-width="90px">
                <el-form-item label="id" prop="用户id">
                    <el-input v-model="form.id" disabled></el-input>
                </el-form-item>
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="form.username" disabled></el-input>
                </el-form-item>
                <el-form-item label="单位名称" prop="surname">
                    <el-input v-model="form.surname" disabled></el-input>
                </el-form-item>
                <el-form-item label="审核状态" prop="check_stat">
                    <el-select v-model="form.check_stat" placeholder="- 审核状态 -" clearable>
                        <el-option v-for="item in sysdict.unitAuthType" :key="item.label" :label="item.name" :value="item.name"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="用户类型" prop="user_type">
                    <el-input v-model="form.user_type" disabled></el-input>
                </el-form-item> 
                <el-form-item label="简介" prop="intro">
                    <el-input v-model="form.intro" disabled></el-input>
                </el-form-item>
                <el-form-item label="国家" prop="country">
                    <el-input v-model="form.country" disabled></el-input>
                </el-form-item>
                <el-form-item label="省份" prop="province" disabled>
                    <el-input v-model="form.province" disabled></el-input>
                </el-form-item>
                <el-form-item label="城市" prop="city" disabled>
                    <el-input v-model="form.city" disabled></el-input>
                </el-form-item>
                <el-form-item label="城区" prop="urban_area">
                    <el-input v-model="form.urban_area" disabled></el-input>
                </el-form-item>
                <el-form-item label="城区编号" prop="city_code">
                    <el-input v-model="form.city_code" disabled></el-input>
                </el-form-item> 
                <el-form-item label="地址" prop="addr">
                    <el-input v-model="form.addr" disabled></el-input>
                </el-form-item>
                <el-form-item label="联系方式" prop="phone">
                    <el-input v-model="form.phone" disabled></el-input>
                </el-form-item>
                <el-form-item label="联系人" prop="contact">
                    <el-input v-model="form.contact" disabled></el-input>
                </el-form-item>
                <el-form-item label="VIP等级" prop="vip_level">
                    <el-input v-model="form.vip_level" disabled></el-input>
                </el-form-item>
                <el-form-item label="已上传资料" prop="imgs">
                    <el-image 
                        style="width: 100px; height: 100px"
                        v-for="(item, index) in form.imgs"
                        :key="index"
                        :src="item.url" 
                        :preview-src-list="imageList">
                    </el-image>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <!-- <el-button type="success" plain @click="editVisible = false">取 消</el-button> -->
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">审 核</el-button>
            </span>
        </el-dialog>

        <!-- 新增弹出框 -->
        <el-dialog title="新增" :visible.sync="addVisible" width="30%">
            <el-form ref="entityAdd" :model="form" :rules="rulesEntityAdd" label-width="90px">
                <el-form-item label="广告类型" prop="ad_type">
                    <el-input v-model="form.ad_type"></el-input>
                </el-form-item>
                <el-form-item label="广告目标" prop="tar">
                    <el-input v-model="form.tar"></el-input>
                </el-form-item>
                <el-form-item label="标题" prop="title">
                    <el-input v-model="form.title"></el-input>
                </el-form-item>
                <el-form-item label="详情" prop="content">
                    <el-input v-model="form.content"></el-input>
                </el-form-item>
                <el-form-item label="管理人" prop="manager">
                    <el-input v-model="form.manager"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import api from '../../../api/index';
import sysdict from '../../../api/sysdict';
import pageUtil from '../../../utils/page';

export default {
    name: 'basetable',
    data() {
        return {
            query: {
                check_stat: '',
                kw: '',
                page: 1,
                size: 10
            },
            tableData: [
                {
                    username: 'user01',
                    surname: '张三',
                    user_group: '普通用户',
                    last_login: '2024-11-01 00:00:00',
                    status: '正常'
                }
            ],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            addVisible: false,
            roles: [],
            rulesEntityAdd: {
                check_stat: [{ required: true, message: '请选择审核状态', trigger: 'change', type: 'string' }]
            },
            rulesEntityEdit: {
                check_stat: [{ required: true, message: '请选择审核状态', trigger: 'change', type: 'string' }]
            },
            statistics: {
                crawlerNum: 12,
                todayAlarm: 3,
                todayTotal: 30,
            },
            loading: false,
            imageList: [],
            sysdict
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            api.getUnitList(this.query).then(res => {
                this.tableData = res.items;
                this.pageTotal = res.total;
            }).catch(err => {
                this.$message.error(err);
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'page', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
            .then(() => {
                api.deleteCastTarget({pk: row.id}).then(res => {
                    this.$message.success('删除成功');
                    this.getData();
                }).catch(err => {
                    this.$message.error(err);
                });
            })
            .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = Object.assign({}, row);
            // this.form.user_group = row.user_role;
            this.editVisible = true;
            this.loadDetail();
        },
        // 保存编辑
        saveEdit() {
            this.$refs.entityEdit.validate(valid => {
                if (valid) {
                    var param = Object.assign({}, {pk: this.form.id, check_stat: this.form.check_stat});
                    api.editUnitStatus(param).then(res => {
                        this.editVisible = false;
                        this.$message.success(`操作成功`);
                        // this.$set(this.tableData, this.idx, this.form);
                        this.getData();
                    }).catch(err => {
                        this.$message.error(err);
                    });
                } else {
                    this.$message.error('请选择审核状态');
                    return false;
                }
            });
        },
        // 新增操作
        handleAdd() {
            this.form = {};
            this.addVisible = true;
            // this.loadRoles();
        },
        saveAdd() {
            this.$refs.entityAdd.validate(valid => {
                if (valid) {
                    api.addCastTarget(Object.assign({}, this.form)).then(res => {
                        this.editVisible = false;
                        this.$message.success(`新增成功`);
                        this.addVisible = false;
                        this.getData();
                    }).catch(err => {
                        this.$message.error(err);
                    });
                } else {
                    return false;
                }
            });
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        },
        handleSizeChange(val) {
            this.$set(this.query, 'size', val);
            this.getData();
        },
        loadDetail() {
            api.getUnitDetail({pk: this.form.id}).then(res => {
                this.imageList = res.item.imgs.map(m => m.url);
            }).catch(err => {
                this.$message.error(err);
            });
        },
        handleAuth() {
            this.loading = true;
            api.editUnitStatus({pk: this.form.id}).then(res => {
                this.imageList = res.item.imgs.map(m => m.url);
                this.loading = false;
            }).catch(err => {
                this.$message.error(err);
                this.loading = false;
            });
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.el-select {
    width: 100%;
}
</style>
